import 'dart:io';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';

/// @date 2020/08/25  16:46
/// @author 程志强
/// @des dio

class DioRoute extends StatefulWidget {
  @override
  _DioRouteState createState() => _DioRouteState();
}

class _DioRouteState extends State<DioRoute> {
  Dio _dio = Dio();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Dio http库"),
      ),
      body: Container(
        alignment: Alignment.center,
        child: FutureBuilder(
          future: _dio.get("https://api.github.com/orgs/flutterchina/repos"),
          builder: (context, snapshot) {
            //完成
            if (snapshot.connectionState == ConnectionState.done) {
              Response response = snapshot.data;
              // 发生错误
              if (snapshot.hasError) {
                return Text(snapshot.error.toString());
              }
              // 成功
              return ListView(
                children: response.data
                    .map<Widget>((e) => ListTile(
                          title: Text(e["full_name"]),
                        ))
                    .toList(),
              );
            }
            // loading
            return CircularProgressIndicator();
          },
        ),
      ),
    );
  }
}
